﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tool;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics;

namespace DataAccess
{
	public class BinarySerialization<D> where D:new()
	{
		// ATTRIBUT
		private string _strDefaultFilePath;
		private string DefaultFilePath
		{
			get 
			{
				if (_strDefaultFilePath == "")
				{
					return Environment.CurrentDirectory+"\\UnknownBinaryFile.bat";
				}
				return _strDefaultFilePath; 
			}
			set { _strDefaultFilePath = value; }
		}

		private static BinaryFormatter _oFormatter;
		private static BinaryFormatter Formatter
		{
		  get { return _oFormatter; }
		  set { _oFormatter = value; }
		}

		// CONSTRCUTEUR
		static BinarySerialization()
		{
			_oFormatter = new BinaryFormatter();
		}

		public BinarySerialization( string strDefaultFilePath )
		{
			_strDefaultFilePath = Environment.CurrentDirectory+strDefaultFilePath;
			_oFormatter = new BinaryFormatter();			
		}

		~BinarySerialization()
		{
			
		}

		//METHODE
		public void Save(D oToSave)
		{
			BinarySerialization<D>.Save(oToSave, DefaultFilePath);
		}

		public static void Save(D oToSave, string strFilePath="")
		{
			try
			{	
				BinaryFormatter formatter = new BinaryFormatter();

				Stream stream = null;
				try
				{
					stream = File.Open(strFilePath, FileMode.Create, FileAccess.Write, FileShare.None);

					// On sérialise
					formatter.Serialize(stream, oToSave);
				}
				catch (Exception e)
				{
					Message.Exception(e);
				}	
				finally
				{
					if( stream != null )
						stream.Close();
				}

			}
			catch (Exception e)
			{					
				Message.Exception(e);
			}				
		}

		public D Load()
		{
			return Load(DefaultFilePath);
		}

		public static D Load(string strFilePath="")
		{
			D oToLoad = new D();

			if( IsExistBinary(strFilePath) )
			{
				BinaryFormatter formatter = new BinaryFormatter();
				FileStream stream = null;
				try
				{
					stream = File.Open(strFilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
					if( stream.Length != 0 )
						oToLoad = (D)formatter.Deserialize(stream);
					stream.Close();
				}
				catch (Exception e)
				{					
					Message.Exception(e);
				}
				finally
				{
					if( stream != null )
						stream.Close();
				}
			}
			return oToLoad;
		}

		public void Clear()
		{
			Clear(DefaultFilePath);
		}

		public static void Clear(string strFilePath = "") 
		{
			if( !IsExistBinary(strFilePath) )
				return;

			D oData = new D();
			Save(oData, strFilePath);
		}

		public void CreateFile()
		{
			CreateFile(DefaultFilePath);
		}

		public static void CreateFile(string strFilePath = "")
		{
			if( IsExistBinary(strFilePath) )
				return;

			VerifFilePath( ref strFilePath );
			
			try
			{
				File.Create(strFilePath);
			}
			catch (Exception e)
			{
				Message.Exception(e);
			}			
			finally
			{
			}
		}	

		public void DeleteFile()
		{			
			DeleteFile(DefaultFilePath);
		}

		public static void DeleteFile(string strFilePath = "")
		{
			if( !IsExistBinary(strFilePath) )
				return;

			VerifFilePath( ref strFilePath );
			
			try
			{
				File.Delete(strFilePath);
			}
			catch (Exception e)
			{
				Message.Exception(e);
			}
		}		

		public bool IsExistBinary()
		{
			return IsExistBinary(DefaultFilePath);
		}

		public static bool IsExistBinary(string strFilePath = "")
		{
			VerifFilePath( ref strFilePath );

			return File.Exists(strFilePath);
		}

		private static void VerifFilePath( ref string strFilePath )
		{			
			Debug.Assert(strFilePath.Length == 0);
		}		
	}
}
